package de.hauck.rtmidlet.views;

import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemCommandListener;
import javax.microedition.lcdui.TextField;

import de.hauck.rtmidlet.DownloadImageThread;
import de.hauck.rtmidlet.RTMidlet;
import de.hauck.rtmidlet.model.Configuration;

public class FormAdHoc extends Form implements CommandListener,
		ItemCommandListener {
	private static Configuration c;
	
	private RTMidlet _main;
	private CanvasCharts _ftCanvas;
	private CanvasImage imageCanvas;
	private String chartName;
	private String[] showStockListArray = { "Realtime", "Time&Sales",
			"Intraday", "FT" };
	private ChoiceGroup cg_charttype = new ChoiceGroup("Charttype",
			Choice.EXCLUSIVE, showStockListArray, null);

	private Command cShow = new Command("Show", Command.OK, 1);
	private Command cBack = new Command("Back", Command.BACK, 1);
	private TextField stockName;

	public FormAdHoc(RTMidlet rtmidlet) {
		super("AdHoc Form");
		// logger = MyLogger.getLogger();
		_main = rtmidlet;
		stockName = new TextField("Stock", "", 15, TextField.ANY);
		append(stockName);
		cg_charttype.setItemCommandListener(this);
		append(cg_charttype);
		addCommand(cShow);
		addCommand(cBack);
		setCommandListener(this);
	}

	public void commandAction(Command command, Displayable dble) {
		if (command == cShow) {
			chartName = stockName.getString();
			int selection = cg_charttype.getSelectedIndex();
			System.out.println("selected: " + selection);
			String url = "";
			switch (selection) {
			case Configuration.RT: {
				url = Configuration.getURL(Configuration.RT, chartName);
				break;
			}
			case Configuration.INTRADAY: {
				url = Configuration.getURL(Configuration.INTRADAY, chartName);
				break;
			}
			case Configuration.TS: {
				url = Configuration.getURL(Configuration.TS, chartName);
				break;
			}
			case Configuration.FINANZTREFF: {
				url = Configuration
						.getURL(Configuration.FINANZTREFF, chartName);
				break;
			}
			}

			System.out.println("Show " + url);
			String[] urls={Configuration.getURL(Configuration.RT, chartName),Configuration.getURL(Configuration.INTRADAY, chartName),Configuration.getURL(Configuration.TS, chartName),Configuration
					.getURL(Configuration.FINANZTREFF, chartName)};
			
			if (chartName != null && !chartName.equals("")) {
				if (null==imageCanvas) imageCanvas = new CanvasImage(this, urls);
				else imageCanvas.setUrls(urls);
				imageCanvas.downloadPic(chartName, selection);
				RTMidlet.getInstance().show(imageCanvas);
			} else {
				RTMidlet.getInstance().setCurrent(this);
				RTMidlet.getInstance().alert("Info", "Please enter a stock symbol.",
						Configuration.ALERT_WAIT_TIME);
			}

		} else if (command == cBack) {
			RTMidlet.getInstance().showMenu();
		}
	}

	public void commandAction(Command c, Item item) {
		// not used
	}

}
